//
//  PlaceHoderTextView.m
//  OutOfParents
//
//  Created by 屠赫宸 on 16/6/7.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//

#import "PlaceHoderTextView.h"
@interface PlaceHoderTextView()
@property(nonatomic,strong)UILabel *placeholderLabel;
@end
@implementation PlaceHoderTextView
-(UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        _placeholderLabel=[[UILabel alloc]initWithFrame:CGRectMake(4, 8, self.bounds.size.width-8, 0)];
        [_placeholderLabel sizeToFit];
        _placeholderLabel.font=[UIFont systemFontOfSize:14];
        _placeholderLabel.textColor=[UIColor grayColor];
        _placeholderLabel.numberOfLines=0;
        [self addSubview:_placeholderLabel];
    }
    return _placeholderLabel;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super initWithCoder:aDecoder]) {
        self.alwaysBounceVertical=YES;//纵向滚动
        self.textColor=[UIColor blackColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(texting) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.alwaysBounceVertical=YES;//纵向滚动
        self.textColor=[UIColor blackColor];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(texting) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
//监听有文字输入
-(void)texting{
    [self setPlaceholderTextShow];
}
-(void)setPlaceholderTextShow{
    self.placeholderLabel.hidden=self.hasText;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.placeholderLabel.frame=CGRectMake(4, 8, self.bounds.size.width-8, 0);
    [self.placeholderLabel sizeToFit];
}
-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder=placeholder;
    self.placeholderLabel.text=placeholder;
    [self setNeedsLayout];
}
-(void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placeholderLabel.font=font;
    [self setNeedsLayout];
}
-(void)setText:(NSString *)text{
    [super setText:text];
    [self setPlaceholderTextShow];
}
-(void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    [self setPlaceholderTextShow];
}
@end
